import axios from 'axios';
import '@/mock/index'

export const getCartData = () => {
  return (dispatch) => {
    axios.get('/api/cart').then(res => {
      const data = res.data;
      const action = initCartData(data)
      dispatch(action)
    })
  }
}

export const initCartData = (data) => {
  return {
    type: 'init_cart_data',
    data
  }
}


export const toCart = (item) => {
  return {
    type: 'to_cart',
    item
  }
}

export const addCartItem = (index) => {
  return {
    type: 'add_cart_item',
    index
  }
}

export const deleteItem = (index) => {
  return {
    type: 'delete_item',
    index
  }
}

export const removeItem = (index) => {
  return {
    type: 'remove_cart_item',
    index
  }
}

export const changeTotalCheck = (flag) => {
  return {
    type: 'change_total_check',
    flag
  }
}

export const changeItemCheck = (opt) => {
  return {
    type: 'change_item_check',
    opt
  }
}